package ex.recruit.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;

import com.guinea.core.PropertyFilterForSql;
import com.guinea.core.utils.ServletUtils;
import com.guinea.core.web.StrutsAction;

import ex.recruit.service.ApplyBaseServiceEx;
import ex.recruit.vo.ApplyJudgmentListVoEx;

public class ApplyProcessReportAction extends StrutsAction {
	protected ApplyBaseServiceEx applyBaseServiceEx;

	public ApplyBaseServiceEx getApplyBaseServiceEx() {
		return applyBaseServiceEx;
	}

	public void setApplyBaseServiceEx(ApplyBaseServiceEx applyBaseServiceEx) {
		this.applyBaseServiceEx = applyBaseServiceEx;
	}

	/**
	 * url参数未定义method时的默认Action函数. 默认为index Action.
	 */
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return list(mapping, form, request, response);
	}

	/**
	 * 列出所有对象的Action函数.
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 从request中获取含属性前缀名的参数,构造去除前缀名后的参数Map.
		Map filterParamMap = ServletUtils.getParametersStartingWith(request,
				"filter_");

		List filters = PropertyFilterForSql.buildFromParaMap(filterParamMap);
		List result = getApplyBaseServiceEx().getApplyProcess(filters);

		request.setAttribute("result", result);
		request.setAttribute("filters", filterParamMap);
		Map data = getApplyBaseServiceEx().getApplyProcessChartData(filters);
		JFreeChart j = createChart(createDataset(data));
		String filename = null;
		try {
			filename = ServletUtilities.saveChartAsPNG(j, 700, 300, request
					.getSession());
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (filename == null)
			filename = "";
		request.setAttribute("filename", filename);
		return mapping.findForward("list");
	}

	private CategoryDataset createDataset(Map data) {
		DefaultCategoryDataset localDefaultCategoryDataset = new DefaultCategoryDataset();
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S1"),
				"待处理", "初审");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S2"),
				"待处理", "走访");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S3"),
				"待处理", "材料审核");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S4"),
				"待处理", "SKN内审");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S5"),
				"待处理", "决策委员会");
		localDefaultCategoryDataset.addValue(0, "通过",
				"初审");
		localDefaultCategoryDataset.addValue(0, "通过",
				"走访");
		localDefaultCategoryDataset.addValue(0, "通过",
				"材料审核");
		localDefaultCategoryDataset.addValue(0, "通过",
				"SKN内审");
		localDefaultCategoryDataset.addValue(0,
				"通过", "决策委员会");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S11"),
				"未通过", "初审");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S12"),
				"未通过", "走访");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S13"),
				"未通过", "材料审核");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S14"),
				"未通过", "SKN内审");
		localDefaultCategoryDataset.addValue((BigDecimal) data.get("S15"),
				"未通过", "决策委员会");
		return localDefaultCategoryDataset;
	}

	private JFreeChart createChart(CategoryDataset paramCategoryDataset) {
		JFreeChart localJFreeChart = ChartFactory.createStackedBarChart(
				"进度统计报表", "", "数量", paramCategoryDataset,
				PlotOrientation.VERTICAL, true, false, false);

		// 配置字体
		Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴
		Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴
		Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部
		Font titleFont = new Font("隶书", Font.BOLD, 25); // 图片标题
		CategoryPlot plot = localJFreeChart.getCategoryPlot();// 图形的绘制结构对象
		plot.setBackgroundPaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.white);
		StackedBarRenderer stackedBarRenderer = new StackedBarRenderer();
		stackedBarRenderer.setSeriesPaint(0, new Color(190, 190, 190));	
		stackedBarRenderer.setSeriesPaint(1, new Color(129, 194, 109));
		stackedBarRenderer.setSeriesPaint(2, new Color(75, 168, 46));
		stackedBarRenderer.setMaximumBarWidth(0.03d);
		stackedBarRenderer.setBaseItemLabelsVisible(true);
		stackedBarRenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		/*stackedBarRenderer
				.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		stackedBarRenderer
				.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());*/
		plot.setRenderer(stackedBarRenderer);
		plot.setBackgroundPaint(Color.WHITE);
		
		plot.setNoDataMessage("没有数据!");
		plot.setNoDataMessageFont(new Font("宋体", Font.BOLD, 25));
		plot.setNoDataMessagePaint(Color.RED);
		// 图片标题
		TextTitle localTextTitle = localJFreeChart.getTitle();
		/*localTextTitle.setBorder(0.0D, 0.0D, 1.0D, 0.0D);
		localTextTitle.setBackgroundPaint(new GradientPaint(0.0F, 0.0F,
				Color.lightGray, 350.0F, 0.0F, Color.white, true));*/
		localTextTitle.setExpandToFitSpace(true);
		localTextTitle.setFont(titleFont);

		// 底部
		localJFreeChart.getLegend().setItemFont(kfont);
		localJFreeChart.getLegend().setHorizontalAlignment(
				HorizontalAlignment.CENTER);

		// X 轴
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLabelFont(xfont);// 轴标题
		domainAxis.setTickLabelFont(xfont);// 轴数值
		domainAxis.setTickLabelPaint(Color.BLUE); // 字体颜色
		domainAxis.setAxisLineVisible(false);
		// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		// // 横轴上的label斜显示

		// Y 轴
		ValueAxis rangeAxis = plot.getRangeAxis();
		rangeAxis.setLabelFont(yfont);
		rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
		rangeAxis.setTickLabelFont(yfont);
		rangeAxis.setAxisLineVisible(false);
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		
		return localJFreeChart;
	}

}
